
let trustyLib  = [];



/**
   key:projectId
   value:{
     trustyId:[]
     isSynchronized:boolean
   }
 */
let map  = new Map();



/**
 * @brief 重置负责人列表
 */
export function resetTrusyLibrary(infos){
  trustyLib  = infos;
}


/**
 * @brief 得到负责人列表
 */
export function getTrustyLibrary(){
  return [...trustyLib];
}


/**
 * @brief 向指定项目添加一个负责人
 */
export function addTrustyToProject(projectId, trustyId){

  if(map.has(projectId) === false){
    map.set(projectId, {isSynchronized:true, trustyList:[]});
  }

  const value  = map.get(projectId);

  const trustyList  = value.trustyList;
  if(trustyList.indexOf(projectId) == -1){
    trustyList.append(trustyId);
  }
  else{
    console.warn("Intent to add repeated trusty to project", projectId);
  }

  if(value.isSynchronized === false){
    value.isSynchronized  = true;
  }
}



export function setTrustyOfProject(projectId, trustyInfos){
  if(map.has(projectId) === false){
    map.set(projectId, {isSynchronized:true, trustyList:[]});
  }

  const value  = map.get(projectId);

  for(let info of trustyInfos){
    value.trustyList.push(info.userId);
  }

  value.isSynchronized  = true;
}


/**
 * @brief 移除指定项目中的某个负责人
 */
export function removeTrustyFromProject(projectId, trustyId){
  const value  = map.get(projectId);
  if( value ){
    const trustyList  = value.trustyList;
    const index  = trustyList.indexOf(trustyId);
    if(index != -1){
      trustyList.splice(index, 1);
    }
  }
}


export function getTrustyIdListOfProject(projectId){
  const value  = map.get(projectId);
  if(value){
    return value.trustyList;
  }
  else{
    return null;
  }
}


/**
 * @brief 根据负责人id得到负责人信息
 */
export function getTrustyInfo(trustyId){
  const trustyInfo  = trustyLib.find( function(elem){ return elem.trustyId === trustyId } );
  return trustyInfo;
}


/**
 * @brief 制定项目的负责人列表是否已经同步
 */
export function isTrustyOfProjectSynchronized(projectId){
  const value  = map.get(projectId);
  if( value ){
    if( value.isSynchronized ){
      return value.isSynchronized;
    }
  }

  return false;
}
